#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1010;
int n, k;
int arr[N];
int f[N];
int main()
{
	while (cin >> n) {
		if (!n) {
			break;
		}
		memset(arr, 0, sizeof(arr));
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; i++) {
			cin >> arr[i];
		}
		cin >> k;
		if (k < 5) {
			cout << k << endl;
			continue;
		}
		sort(arr + 1, arr + 1 + n);
		int m = k - 5;
		for (int i = 1; i < n; i++) {
			for (int j = m; j >= arr[i]; j--) {
				f[j] = max(f[j], f[j - arr[i]] + arr[i]);
			}
		}
		cout << k - f[m] - arr[n] << endl;
	}
	return 0;
}